package parserdecoder

import (
	"fmt"
	"regexp"
	"strings"
)

// bss:=regCompiled.Find(bs,-1)
// bss:=regCompiled.FindAll(bs,-1)
// bss:=regCompiled.FindSubmatch(s,-1) //[0]整个正则匹配的, [1]括号内匹配的. [n]第n个括号?
// bss:=regCompiled.FindAllSubmatch(s,-1) //FindSubmatch的All版本, (FindSubmatch 匹配到一个即结束)

// bss:=regCompiled.FindStringSubmatch(s,-1)
// bss:=regCompiled.FindAllString(s,-1) // []byte版->string版
// bss:=regCompiled.FindAllStringSubmatch(s,-1)

const (
	IPV6_STR = "🌀"
)

func New_name_replaceIPV6() func(name, newStr string) string {
	ipv6, _ := regexp.Compile(`(?i)[,\.\*\+\?\|\^\$\{\}\(\)\[\]\\《》〈〉「」【】〖〗\$\-=:]*ipv6[,\.\*\+\?\|\^\$\{\}\(\)\[\]\\《》〈〉「」【】〖〗\$\-=:]*`)
	return func(name, newStr string) string {
		return ipv6.ReplaceAllLiteralString(name, newStr)
	}
}

// func name_replaceIPV6(name string) string {
// 	return strings.ReplaceAll(name, "ipv6", IPV6_STR)
// }

func New_channelnameTrans() func(name string) (newName string) {
	re, _ := regexp.Compile(`cctv[^0-9]*([0-9]+)`)
	reCCTV5, _ := regexp.Compile(`cctv[^0-9]*0?5 *(\+|p)`)

	replaceStart, _ := regexp.Compile(`^[,\.\*\+\?\|\^\$\{\}\(\)\[\]\\《》〈〉「」【】〖〗\$\-=:]`)
	replaceEnd, _ := regexp.Compile(`[,\.\*\+\?\|\^\$\{\}\(\)\[\]\\《》〈〉「」【】〖〗\$\-=:]$`)
	// replace, _ := regexp.Compile(`[,\.\*\+\?\|\^\$\{\}\(\)\[\]\\《》〈〉「」【】〖〗\$\-=:]`)

	// if err != nil {
	// 	return func(name string) (name1 string) {
	// 		return name
	// 	}
	// }

	return func(name string) (newName string) {
		cctv := func(s, s2 string) string {
			s = strings.TrimLeft(s, "0")
			switch s {
			case "1":
				return "1 综合"
			case "2":
				return "2 财经"
			case "3":
				return "3 综艺"
			case "4":
				return "4 中文国际"
			case "5":
				// if strings.Contains(s2, "5+") || strings.Contains(s2, "5p") {
				if len(reCCTV5.FindString(s2)) > 0 {
					return "5+体育赛事"
				}
				return "5 体育"
			case "6":
				return "6 电影"
			case "7":
				return "7 国防军事"
			case "8":
				return "8 电视剧"
			case "9":
				return "9 记录"
			case "10":
				return "10 科教"
			case "11":
				return "11 戏曲"
			case "12":
				return "12 社会与法"
			case "13":
				return "13 新闻"
			case "14":
				return "14 少儿"
			case "15":
				return "15 音乐"
			case "16":
				return "16 奥运"
			case "17":
				return "17 农村"
				// case "18":
				// 	return "18"
				// case "19":
				// 	return "19"
				// case "20":
				// 	return "综合"
				// case "21":
				// 	return "综合"
				// case "22":
				// 	return "综合"
				// case "23":
				// 	return "综合"
				// case "24":
				// 	return "综合"
			}
			return s
		}

		if len(name) == 0 {
			return name
		}

		if len(name) > 100 {
			name = name[:100]
		}

		name = replaceStart.ReplaceAllLiteralString(name, "")
		name = replaceEnd.ReplaceAllLiteralString(name, "")

		lower := strings.ToLower(name)
		if strings.Contains(lower, "cctv") {
			mathed := re.FindStringSubmatch(lower)
			if mathed == nil {
				return name
			}
			if strings.Contains(lower, "ipv6") {
				newName = fmt.Sprintf("cctv %s %s", cctv(mathed[1], lower), IPV6_STR)
			} else {
				newName = fmt.Sprintf("cctv %s", cctv(mathed[1], lower))
			}
			return

		}
		newName = name
		return
	}
}
